tinyMCE.init({
	mode : "textareas",
	relative_urls : false,
	remove_script_host : false,
	document_base_url :base_url,
	theme : "advanced",
	valid_elements : "@[id|style|title|dir<ltr?rtl|lang|"+ "onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|"
	+ "onkeydown|onkeyup],a[rel|rev|charset|hreflang|tabindex|accesskey|type|"
	+ "name|href|target|title|class|onfocus|onblur],strong/b,em/i,strike,u,"
	+ "#p,-ol[type|compact],-ul[type|compact],-li,br,img[longdesc|usemap|"
	+ "src|border|alt=|title|hspace|vspace|width|height|align],-sub,-sup,"
	+ "-blockquote,-table[border=0|cellspacing|cellpadding|width|frame|rules|"
	+ "height|align|summary|bgcolor|background|bordercolor],-tr[rowspan|width|"
	+ "height|align|valign|bgcolor|background|bordercolor],tbody,thead,tfoot,"
	+ "#td[colspan|rowspan|width|height|align|valign|bgcolor|background|bordercolor"
	+ "|scope],#th[colspan|rowspan|width|height|align|valign|scope],caption,-div,"
	+ "-span,-code,-pre,address,-h1,-h2,-h3,-h4,-h5,-h6,hr[size|noshade],-font[face"
	+ "|size|color],dd,dl,dt,cite,abbr,acronym,del[datetime|cite],ins[datetime|cite],"
	+ "object[classid|width|height|*],param[name|value|_value],embed[type|width"
	+ "|height|src|*],script[src|type],map[name],area[shape|coords|href|alt|target],bdo,"
	+ "button,col[align|char|charoff|span|valign|width],colgroup[align|char|charoff|span|"
	+ "valign|width],dfn,fieldset,form[action|accept|accept-charset|enctype|method],"
	+ "input[accept|alt|checked|disabled|maxlength|name|readonly|size|src|type|value],"
	+ "kbd,label[for],legend,noscript,optgroup[label|disabled],option[disabled|label|selected|value],"
	+ "q[cite],samp,select[disabled|multiple|name|size],small,"
	+ "textarea[cols|rows|disabled|name|readonly],tt,var,big",
	width:"100%",
	height: "600px",
	plugins : "paste,inlinepopups",
	theme_advanced_buttons1 : "bold,italic,underline,separator,strikethrough,justifyleft,justifycenter,justifyright, justifyfull,bullist,numlist",
	theme_advanced_buttons2:"",
	theme_advanced_buttons3:"",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_font_sizes : "3,4,5,6",
	entity_encoding : "raw",
	language : 'vi',
	theme_advanced_buttons1_add : "fontselect,fontsizeselect,forecolor,backcolor,separator,link,unlink,pasteword,removeformat",
	theme_advanced_path_location : "bottom",
	theme_advanced_path : false,
	theme_advanced_resize_horizontal : false,
	theme_advanced_resizing : true,
	forced_root_block : false,
	force_p_newlines : true,
	apply_source_formatting : true
});


jQuery(document).ready(function(){

	$("input[type=radio][name=typing_method]").click(function(){
		Mudim.SetMethod(parseInt($(this).val()));
	});


	jQuery.validator.addMethod("alpha", function(value, element) {
		if(!this.optional(element)){
			var false_string = "!@#$^&'`~<>*()+={[}]?/;\"";
			var the_char = ""
			for(i=0;i<false_string.length;i++){
				the_char=false_string.charAt(i);
				 if(value.indexOf(the_char)!=-1 ){
					return false;
				}
			}
			return true;
		}},'Tiều đề tin không được có các ký tự đặc biệt.'
	);

	jQuery.validator.addMethod("validate_content", function(value, element) {
			var content = tinyMCE.get('post_content').getContent();
			if(content=="" || content.length <=200){
				return false;
			}
			return true;
		},'Nội dung phải có ít nhất 200 ký tự.'
	);
	jQuery.validator.addMethod("dautiengviet", function(value, element) {
		if(!this.optional(element))
		{
			var matchs =  'ÀÁÂÃÈÉÊÌÍÒÓÔÕÙÚĂĐĨŨƠàáâãèéêìíòóôõùúăđĩũơƯĂẠẢẤẦẨẪẬẮẰẲẴẶẸẺẼỀỀỂưăạảấầẩẫậắằẳẵặẹẻẽềềểỄỆỈỊỌỎỐỒỔỖỘỚỜỞỠỢỤỦỨỪễệỉịọỏốồổỗộớờởỡợụủứừỬỮỰỲỴÝỶỸửữựỳỵỷỹ';
			var the_char = "";
			for(i=0;i<matchs.length;i++){
				the_char=matchs.charAt(i);
				 if(value.indexOf(the_char)!=-1 ){
					return true;
				}
			}
			return false;
		}},'Tiều đề tin phải là tiếng việt có dấu.'
	);


        $("#postForm").validate({
            rules: {
               name: {
                    required: true,
                    alpha: true,
                    dautiengviet: true,
                    minlength:20,
                    maxlength:120
                },
                category: {
                    required: true,
                    min:1
                },
                content : {
                    validate_content:true
                },
                provinder: {
                    required: true,
                    min:1
                },
                captcha: {
                    required:true,
                    remote: base_url+"ma-bao-mat/kiem-tra"
                }
            },
            messages: {
                name: {
                    required: 'Xin hãy nhập tiêu đề tin',
                    minlength: jQuery.format("Tiêu đề tin phải có ít nhất {0} ký tự"),
                    maxlength: "Tiêu để của tin có độ dài đến 120 ký tự"
                },

                category: {
                    required: "Xin hãy chọn chuyên mục",
                    min: "Bạn chưa chọn chuyên mục"
                },
                provinder: {
                    required: "Xin hãy chọn nơi rao",
                    min: "Bạn chưa chọn nơi rao"
                },
                captcha: {
                    required: "Xin hãy nhập mã bảo mật",
                    remote: "Mã bảo mật không đúng"
                }
            },
            errorPlacement: function(error,element){ },
            invalidHandler: function(form, validator) {
                var errors = validator.numberOfInvalids();
                if (errors) {
                    alert(validator.errorList[0].message);  //Only show first invalid rule message!!!
                    validator.errorList[0].element.focus(); //Set Focus
                }
            },
            submitHandler: function(form) {
                form.submit();
                  return false;
             }
        });

});